package edu.byui.chat;

import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class GetData {
	private Connection con;
	private String url;

	public GetData() {
		url = "jdbc:mysql://localhost:3306/cit310";
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
		}
		catch(Exception e) {
			System.out.println("you Had some other error, not a database error");
			e.printStackTrace();
		}
	}

	public void queryTest(String prepStatement, ArrayList<Object> values) {
		try {
			con = DriverManager.getConnection(url, "javadbc", "cit310");
			PreparedStatement updateData = con.prepareStatement(prepStatement);
			for(int i = 0; i<values.size(); i++) {
				int index = i + 1;
				Object obj = values.get(i);
				if(obj instanceof String) {
					updateData.setString(index, (String)obj);
				}
				else if(obj instanceof Integer) {
					updateData.setInt(index, (Integer)obj);
				}
				else if(obj instanceof GregorianCalendar) {
					updateData.setString(index, MySQLConversion.gregorToDateTime((GregorianCalendar)obj));
				}
			}
			ResultSet set = updateData.executeQuery();
			while(set.next()) {
				String val = set.getString(1);
				System.out.println("val = " + val);
			}
			con.close();
		}
		catch(SQLException e) {
			System.out.println("You had a database Error");
			e.printStackTrace();
		}
		catch(Exception e) {
			System.out.println("You had some other error, not a database error");
			e.printStackTrace();
		}
	}

	private ResultSet query(String prepStatement, ArrayList<Object> values) {
		ResultSet set = null;
		try {
			con = DriverManager.getConnection(url, "javadbc", "cit310");
			PreparedStatement updateData = con.prepareStatement(prepStatement);
			for(int i = 0; i < values.size();i++) {
				int index = i + 1;
				Object obj = values.get(i);
				if(obj instanceof String) {
					updateData.setString(index, (String)obj);
				}
				else if(obj instanceof Integer) {
					updateData.setInt(index, (Integer)obj);
				}
				else if(obj instanceof GregorianCalendar) {
					updateData.setString(index, MySQLConversion.gregorToDateTime((GregorianCalendar)obj));
				}
			}
			set = updateData.executeQuery();
		}
		catch(SQLException e) {
			System.out.println("You had a database Error");
			e.printStackTrace();
		}
		catch(Exception e) {
			System.out.println("You Had some other error, not a database error");
			e.printStackTrace();
		}
		return set;
	}

	public ArrayList<Message> getMessagesFor(String username) {
		ArrayList<Object> params = new ArrayList<Object>();
		ArrayList<Message> queue = new ArrayList<Message>();
		params.add(username);
		ResultSet result = this.query(
				"SELECT message_id, message_from, message_to, message_body, message_sent, message_read "
				+ "FROM message "
				+ "WHERE message_to = ?;", params);
		try {
			while(result.next()) {
				queue.add(new Message(result.getInt(1), result.getString(2), result.getString(3), result.getString(4), MySQLConversion.dateTimeToGregor(result.getString(5)), result.getInt(6)));
			}
			con.close();
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
		return queue;
	}
}
